package top.kaoshanji.r5ch12f;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Connection;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/5/11 14:46
 */
@Service("jdbcUserServiceV2")
public class JdbcUserServiceV2 {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Transactional
    public void logon(String userName) {
        try {

            // 代码清单-r5ch12f-422页a：使用DataSourceUtils获取数据连接
            Connection connection = DataSourceUtils.getConnection(jdbcTemplate.getDataSource());

            String sql = " UPDATE t_user1 SET last_logon_time=? WHERE user_name =? ";
            jdbcTemplate.update(sql, System.currentTimeMillis(), userName);
            Thread.sleep(1000);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 以异步线程的方式执行JdbcUserServiceV2#logon()方法，以模拟多线程的环境
    public static void asynchrLogon(JdbcUserServiceV2 userService, String userName) {
        UserServiceRunner runner = new UserServiceRunner(userService, userName);
        runner.start();
    }


    private static class UserServiceRunner extends Thread {
        private JdbcUserServiceV2 userService;
        private String userName;

        public UserServiceRunner(JdbcUserServiceV2 userService, String userName) {
            this.userService = userService;
            this.userName = userName;
        }

        public void run() {
            userService.logon(userName);
        }
    }

    // 让主执行线程睡眠一段指定的时间
    public static void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // 汇报数据源的连接占用情况
    public static void reportConn(BasicDataSource basicDataSource) {
        System.out.println("...连接数[活跃：空闲]-[ " + basicDataSource.getNumActive() + "：" + basicDataSource.getNumIdle() + "]...");
    }

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("r5ch12f/application-context-v2.xml");

        JdbcUserServiceV2 userService = (JdbcUserServiceV2)context.getBean("jdbcUserServiceV2");
        BasicDataSource basicDataSource = (BasicDataSource)context.getBean("dataSource");

        JdbcUserServiceV2.reportConn(basicDataSource);

        JdbcUserServiceV2.asynchrLogon(userService, "tom");
        JdbcUserServiceV2.sleep(500);
        JdbcUserServiceV2.reportConn(basicDataSource);


        JdbcUserServiceV2.sleep(2000);
        JdbcUserServiceV2.reportConn(basicDataSource);


        JdbcUserServiceV2.asynchrLogon(userService, "john");
        JdbcUserServiceV2.sleep(500);
        JdbcUserServiceV2.reportConn(basicDataSource);


        JdbcUserServiceV2.sleep(2000);
        JdbcUserServiceV2.reportConn(basicDataSource);

    }


}
